/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.umlutils;

import org.eclipse.uml2.uml.ActivityFinalNode;
import org.eclipse.uml2.uml.ActivityParameterNode;
import org.eclipse.uml2.uml.ControlFlow;
import org.eclipse.uml2.uml.InitialNode;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.LiteralNull;
import org.eclipse.uml2.uml.OutputPin;
import org.eclipse.uml2.uml.UMLPackage;

/**
 * This class encapsulates an <code>org.eclipse.uml2.uml.Activity</code><BR>
 * 
 * @author Remi SCHNEKENBURGER
 */
public class Activity extends Behavior {

    /**
     * 
     */
    private org.eclipse.uml2.uml.Activity uml2Activity;
    
    /**
     * 
     * 
     * @param activity 
     */
    public Activity(org.eclipse.uml2.uml.Activity activity) {
        super(activity);
        this.uml2Activity = activity;
    }
    
    /**
     * Get the encapsulated <code>Activity</code>.
     * 
     * @return Returns the uml2Activity.
     */
    public org.eclipse.uml2.uml.Activity getUml2Activity() {
        return uml2Activity;
    }
    
    /**
     * Creates a CreateObjectAction.
     * 
     * @param classifier the type of the created object
     * @param resultName the name of the output pin 'result'
     * @param name the name of the action
     * 
     * @return the created Action
     * 
     * @see org.eclipse.uml2.uml.CreateObjectAction
     */
    public com.cea.papyrus.umlutils.CreateObjectAction createCreateObjectAction(String name, String resultName, com.cea.papyrus.umlutils.Class classifier) {
        org.eclipse.uml2.uml.CreateObjectAction uml2CreateObjectAction = (org.eclipse.uml2.uml.CreateObjectAction)(uml2Activity.createNode(name, UMLPackage.eINSTANCE.getCreateObjectAction()));
        if(uml2CreateObjectAction != null) {
            uml2CreateObjectAction.setClassifier(classifier.getUml2Class());
            // create result output pin
            OutputPin result = uml2CreateObjectAction.createResult(resultName, classifier.getUml2Class());
            result.setName(resultName);
            // set upper bound (necessary, or model is not valid
            LiteralInteger upperBound = (LiteralInteger)result.createUpperBound("", null, UMLPackage.eINSTANCE.getLiteralInteger());
            upperBound.setValue(1);
            result.setUpperBound(upperBound);
        }
        return new com.cea.papyrus.umlutils.CreateObjectAction(uml2CreateObjectAction);
    }
    
    /**
     * Creates a ControlFlow between 2 ActivityNodes.
     * 
     * @param targetNode the target ActivityNode
     * @param sourceNode the source ActivityNode
     * 
     * @return the created ControlFlow
     */
    public com.cea.papyrus.umlutils.ControlFlow createControlFlow(com.cea.papyrus.umlutils.ActivityNode sourceNode, com.cea.papyrus.umlutils.ActivityNode targetNode) {
        ControlFlow uml2ControlFlow = (org.eclipse.uml2.uml.ControlFlow)(uml2Activity.createEdge("ControlFlow_"+sourceNode.getName()+"_"+targetNode.getName(), UMLPackage.eINSTANCE.getControlFlow()));
        
        if(uml2ControlFlow != null) {
            uml2ControlFlow.setSource(sourceNode.getUml2ActivityNode());
            uml2ControlFlow.setTarget(targetNode.getUml2ActivityNode());
            // set the guard and the weight (necessary for a valid model)
            LiteralNull guard = (LiteralNull)uml2ControlFlow.createGuard("", null, UMLPackage.eINSTANCE.getLiteralNull());
            uml2ControlFlow.setGuard(guard);
            LiteralInteger weight = (LiteralInteger)uml2ControlFlow.createWeight("", null, UMLPackage.eINSTANCE.getLiteralInteger());
            weight.setValue(1);
            uml2ControlFlow.setWeight(weight);
        }
        return new com.cea.papyrus.umlutils.ControlFlow(uml2ControlFlow);
    }
    
    /**
     * Creates an InitialNode.
     * 
     * @param name the name of the InitialNode
     * 
     * @return the created InitialNode
     */
    public com.cea.papyrus.umlutils.InitialNode createInitialNode(String name) {
        InitialNode uml2initialNode = (org.eclipse.uml2.uml.InitialNode)(uml2Activity.createNode(name, UMLPackage.eINSTANCE.getInitialNode()));
       
        return new com.cea.papyrus.umlutils.InitialNode(uml2initialNode);
    }
    
    /**
     * Creates an ActivityFinalNode.
     * 
     * @param name the name of the object
     * 
     * @return the created ActivityFinalNode
     */
    public com.cea.papyrus.umlutils.ActivityFinalNode createActivityFinalNode(String name) {
        ActivityFinalNode uml2ActivityFinalNode = (org.eclipse.uml2.uml.ActivityFinalNode)(uml2Activity.createNode(name, UMLPackage.eINSTANCE.getActivityFinalNode()));    
      
        return new com.cea.papyrus.umlutils.ActivityFinalNode(uml2ActivityFinalNode);
    }
    
    /**
     * Creates an ActivityParameterNode.
     * 
     * @param operationParameter 
     * @param name the name of the object
     * 
     * @return the created ActivityFinalNode
     */
    public com.cea.papyrus.umlutils.ActivityParameterNode createActivityParameterNode(String name, Parameter operationParameter) {
        ActivityParameterNode uml2ActivityParameterNode = (org.eclipse.uml2.uml.ActivityParameterNode)(uml2Activity.createNode(name, UMLPackage.eINSTANCE.getActivityParameterNode()));    
       
        if(uml2ActivityParameterNode!=null) {
            uml2ActivityParameterNode.setType(operationParameter.getUml2Parameter().getType());
            org.eclipse.uml2.uml.Parameter newParam = uml2Activity.createOwnedParameter(name, operationParameter.getUml2Parameter().getType());
            newParam.setDirection(operationParameter.getUml2Parameter().getDirection());
            newParam.setEffect(operationParameter.getUml2Parameter().getEffect());
            uml2ActivityParameterNode.setParameter(newParam);
        }
        return new com.cea.papyrus.umlutils.ActivityParameterNode(uml2ActivityParameterNode);
    }
}

